package org.jeecg;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.api.client.util.Maps;
import com.google.common.collect.Lists;
import com.google.zxing.common.detector.MathUtils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import io.finer.erp.jeecg.bas.entity.BasMaterial;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		QrCodeUtil.generate("M®608501109", 300, 300, FileUtil.file("d:/sss.png"));
		
//		Map<BasMaterial, String> map = Maps.newHashMap();
//		BasMaterial mate = new BasMaterial();
//		mate.setId("123");
//		map.put(mate, "123");
//		
//		BasMaterial mate1 = new BasMaterial();
//		mate1.setId("123");
//		System.out.println(map.get(mate1));
//		System.out.println(mate.hashCode());
//		System.out.println(mate1.hashCode());
//		
//		System.out.println("01001100100101011010010001001111100011111001110110000000000001".length());
		
		List<String> list = Lists.newArrayList();
		for (String string : list) {
			System.out.println(string);
		}
		
//		System.out.println(new BigDecimal("15.200").stripTrailingZeros().toString());
		List<BigDecimal> decimals = Lists.newArrayList();
		BigDecimal decimal = new BigDecimal("237913");
		while(true) {
			BigDecimal[] aa = getLess(decimal);
			if(aa[1].compareTo(decimal) == 0) {
				decimals.add(decimal);
				break;
			}else {
				decimals.add(aa[0]);
				decimal = aa[1];
			}
		}
		Map<BigDecimal, List<BigDecimal>> maps = Maps.newLinkedHashMap();
		for (BigDecimal bigDecimal : decimals) {
//			System.out.println(bigDecimal.toPlainString());
			List<BigDecimal> des = maps.get(bigDecimal);
			if(des == null) {
				des = Lists.newArrayList();
				des.add(bigDecimal);
				maps.put(bigDecimal, des);
			} else {
				des.add(bigDecimal);
			}
		}
		for (BigDecimal bigDecimal : maps.keySet()) {
			System.out.println(bigDecimal.toString() + "   " + maps.get(bigDecimal).size());
		}
	}

	public static BigDecimal[] getLess(BigDecimal decimal) {
		BigDecimal sqrt = sqrt(decimal, 0);
		for (BigDecimal i = new BigDecimal(2); i.compareTo(sqrt) <= 0; i = i.add(BigDecimal.ONE)) {
			BigDecimal[] c = decimal.divideAndRemainder(i);
			BigDecimal ss = c[1];
			if(ss.compareTo(BigDecimal.ZERO) == 0) {
				BigDecimal ss1 = c[0];
				return new BigDecimal[]{new BigDecimal(i.toString()), ss1};
			}
		}
		
		return new BigDecimal[]{BigDecimal.ONE, decimal};
	}

	public static BigDecimal sqrt(BigDecimal value, int scale){
	    BigDecimal num2 = BigDecimal.valueOf(2);
	    int precision = 100;
	    MathContext mc = new MathContext(precision, RoundingMode.HALF_UP);
	    BigDecimal deviation = value;
	    int cnt = 0;
	    while (cnt < precision) {
	        deviation = (deviation.add(value.divide(deviation, mc))).divide(num2, mc);
	        cnt++;
	    }
	    deviation = deviation.setScale(scale, BigDecimal.ROUND_HALF_UP);
	    return deviation;
	}
}
